package com.servingcloud.serverb.modules.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.servingcloud.serverb.common.model.RestPage;
import com.servingcloud.serverb.common.model.RestResult;
import com.servingcloud.serverb.modules.entity.Company;
import com.servingcloud.serverb.modules.model.CompanyMODIFY;
import com.servingcloud.serverb.modules.model.CompanyQUERY;
import com.servingcloud.serverb.modules.model.CompanySAVE;
import com.servingcloud.serverb.modules.service.CompanyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RefreshScope
@RequestMapping("/company")
@Api(value="公司信息接口", tags="公司")
public class CompanyController {
	/*@Value("${name}")
    private  String name;
    @Value("${age}")
    private  String age;
    @Value("${version}")
    private  String version="开发环境";
    @ApiIgnore //不显示该方法在api上
    @RequestMapping("/test")
    public String test(){
        return "a你好，我是"+name+",年龄："+age+"岁。当前环境："+version;
    }*/
    @Autowired
    private CompanyService companyService;
    @Autowired HttpServletRequest request;
    private String getHostInfo() {
    	return request.getLocalName() +":"+request.getServerPort();
    }
    @ApiOperation(httpMethod="GET", value="获取服务器信息", notes="", response=String.class)
    @RequestMapping("/test")
    public String test(){
        return "我的地址："+getHostInfo();
    }
    
    @ApiOperation(httpMethod="POST", value="获取公司列表分页", notes="", response=Company.class)
    @RequestMapping("/findCompanyPage")
    public @ResponseBody RestResult<RestPage<Company>> findCompanyPage(@RequestBody CompanyQUERY param){
    	RestResult<RestPage<Company>> ret = new RestResult<RestPage<Company>>();
    	ret.setSuccess(true);
    	ret.setErrorMsg(getHostInfo());
    	try {
    		ret.setReturnValue(companyService.findCompanyPage(param));
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(e.getMessage());
    	}
    	return ret;
    }
    @ApiOperation(httpMethod="POST", value="新增公司信息", notes="", response=Long.class)
    @RequestMapping("/saveCompany")
    public @ResponseBody RestResult<Long> saveCompany(@RequestBody @Validated CompanySAVE param){
    	RestResult<Long> ret = new RestResult<Long>();
    	ret.setSuccess(true);
    	ret.setErrorMsg(getHostInfo());
    	try {
    		ret.setReturnValue(companyService.saveCompany(param));
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(e.getMessage());
    	}
    	return ret;
    }
    @ApiOperation(httpMethod="POST", value="修改公司信息", notes="")
    @RequestMapping("/modifyCompany")
    public @ResponseBody RestResult<Object> modifyCompany(@RequestBody @Validated CompanyMODIFY param){
    	RestResult<Object> ret = new RestResult<Object>();
    	ret.setSuccess(true);
    	ret.setErrorMsg(getHostInfo());
    	try {
    		companyService.modifyCompany(param);
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(e.getMessage());
    	}
    	return ret;
    }
    @ApiOperation(httpMethod="POST", value="删除公司", notes="")
    @RequestMapping("/deleteCompany")
    public @ResponseBody RestResult<Object> deleteCompany(@RequestBody @Validated CompanyMODIFY param){
    	RestResult<Object> ret = new RestResult<Object>();
    	ret.setSuccess(true);
    	ret.setErrorMsg(getHostInfo());
    	try {
    		companyService.deleteCompany(param);
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(e.getMessage());
    	}
    	return ret;
    }
}
